home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / knowhow4 / variable.h < prev    next >
C/C++ Source or Header  |  1994-10-22  |  1KB  |  60 lines

  1. #ifndef __VARIABLE_H_
  2. #define __VARIABLE_H_
  3.  
  4. /*  Simple BASIC-like language. double, double[] and char[] (strings) are
  5. supported.
  6. */
  7.  
  8. #include "simple.h"
  9. #include "kh_error.h"
  10. #include <string.h>
  11.  
  12. // We use dynamically allocated arrays of double. Format is:
  13. // d[0] == number of elements, d[1 - n] contains data.
  14. enum VAR_TYPE { REAL = 1, STR, ARRAY };
  15.  
  16. struct Variable
  17.     {
  18.     VAR_TYPE type;
  19.     char* name;
  20.     union
  21.         {
  22.         double d;
  23.         char* s;
  24.         double* da;
  25.     };
  26.     Variable(double D, char* s)
  27.         { type = REAL; d = D; name = strdup(s); }
  28.     Variable(char*  S, char* nm)
  29.         { type = STR; s = strdup(S); name = strdup(nm); }
  30.     Variable(double* D, char* s)
  31.         { type = ARRAY; da = new double[D[0] + 1];
  32.       memcpy(da, D, (D[0] + 1) * sizeof(double));
  33.       da[0] = (ulong)(D[0] + 1); name = strdup(s); }
  34.     Variable(int dim, char* s)
  35.         {
  36.     type = ARRAY; da = new double[dim + 1];
  37.     memset((double*)da, 0, dim * sizeof(double));
  38.     da[0] = (ulong)dim;
  39.     name = strdup(s);
  40.     }
  41.  
  42.     ~Variable()
  43.         {
  44.         delete name;
  45.         name = NULL;
  46.     if(type == STR)
  47.         {
  48.         delete s;
  49.         s = NULL;
  50.         }
  51.     else if(type == ARRAY)
  52.         {
  53.         delete da;
  54.         da = NULL;
  55.         }
  56.         }
  57.  
  58.     };
  59.  
  60. #endif __VARIABLE_H_